package com.lzz.bottomencapsulation.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lzz.bottomencapsulation.holder.MvpRVViewHolder;

import java.util.List;


/**
 * <p>RecyclerView  的父适配器</p>
 */

public abstract   class MvpBaseRecyclerAdapter<V extends MvpRVViewHolder> extends RecyclerView.Adapter<V> {

    protected OnItemClickListener onItemClickListener;//itemview点击事件
    OnItemLongClickListener onItemLongClickListener;//itemview长按事件
    protected View.OnClickListener onItemViewClickListener;//itemview内控件点击事件
    View.OnLongClickListener onItemViewLongClickListener;//itemview内控件长按事件
    protected Activity activity;
    protected Context context;

    public MvpBaseRecyclerAdapter(Context context) {
        this.context = context;
    }

    public MvpBaseRecyclerAdapter(Context context,OnItemClickListener onItemClickListener) {
        this(context,null,onItemClickListener,null,null);
    }

    public MvpBaseRecyclerAdapter(Context context, View.OnClickListener onItemViewClickListener) {
        this(context,onItemViewClickListener,null,null,null);
    }

    public MvpBaseRecyclerAdapter(Context context, View.OnClickListener
            onItemViewClickListener, OnItemClickListener onItemClickListener) {
        this(context,onItemViewClickListener,onItemClickListener,null,null);
    }

    public MvpBaseRecyclerAdapter(Context context, View.OnClickListener
            onItemViewClickListener, OnItemClickListener onItemClickListener,OnItemLongClickListener onItemLongClickListener,View.OnLongClickListener onItemViewLongClickListener) {
        this.context = context;
        this.onItemViewClickListener = onItemViewClickListener;
        this.onItemClickListener = onItemClickListener;
        this.onItemLongClickListener = onItemLongClickListener;
        this.onItemViewLongClickListener = onItemViewLongClickListener;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public Activity getActivity() { return activity; }

    public Context getContext() { return context; }

    @Override
    public void onBindViewHolder(@NonNull V v, int i) {
        initListener(v,i);
        onBindView(v,i);
    }

    @Override
    public void onBindViewHolder(V holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()){
            this.onBindViewHolder(holder, position);
        }else{
        onBindView(holder,position,payloads);
        }
    }

    /**
     * 初始化监听
     * @param holder
     * @param position
     */
    protected void initListener(final V holder, final int position) {
        if (onItemClickListener != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v,position);
                }
            });
        if (onItemLongClickListener!=null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickListener.onItemLongClick(v,position);
                    return false;
                }
            });
        }
        if (onItemViewClickListener!=null){
            holder.setOnClickListener(onItemViewClickListener);
        }
        if (onItemViewLongClickListener!=null){
            holder.setOnLongClickListener(onItemViewLongClickListener);
        }
    }


    /**
     * item点击事件回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * item长按事件回调接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    /**
     * 初始化，adapter整体刷新
     * @param v
     * @param i
     */
    public abstract void onBindView(@NonNull V holder, int position);

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 初始化，adapter局部刷新
     * @param holder
     * @param position
     * @param payloads
     */
    public abstract void onBindView(V holder, int position, List<Object> payloads);

    /**
     * 局部刷新，单个itemview
     * @param position
     */
    public void onRefresh(int position){
        this.notifyItemChanged(position,"onRefresh");
    }

}
